home *** CD-ROM | disk | FTP | other *** search
/ Windows 95 API Bible / Windows 95 API Bible 3 Disc Set.iso / Win32 API Bible Book 1 of 3.iso / chapte25 / ex15.c < prev    next >
C/C++ Source or Header  |  1995-04-23  |  3KB  |  78 lines

  1. #include <genstub.c>
  2.  
  3. // Define these menu options in a popup menu.
  4. #define IDM_WORST    401
  5. #define IDM_LOW      402
  6. #define IDM_NORMAL   403
  7. #define IDM_HIGH     404
  8. #define IDM_BEST     405
  9.  
  10. //  Function that wastes CPU time.
  11. DWORD GoWasteSomeCPU(void)
  12. {
  13.     long i, j;
  14.     DWORD dwTicksNow = GetTickCount( );
  15.     for ( i = 0; i < 100000; i ++ )
  16.         for ( j = 0; j < 1000; j ++ );
  17.     return GetTickCount( ) - dwTicksNow;
  18. }
  19.  
  20. // Windows Message Procedure.
  21. LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
  22. {
  23.     switch (uMsg)
  24.     {
  25.             case WM_COMMAND:       // process menu items
  26.                 {
  27.                     HANDLE hThread = GetCurrentThread();
  28.                     switch ( LOWORD( wParam ) )
  29.                     {
  30.                         case IDM_WORST:
  31.                             SetThreadPriority( hThread, THREAD_PRIORITY_LOWEST );
  32.                             SendMessage( hWnd, WM_USER, 0, GoWasteSomeCPU() );
  33.                             break;
  34.                         case IDM_LOW:
  35.                             SetThreadPriority( hThread, THREAD_PRIORITY_BELOW_NORMAL );
  36.                             SendMessage( hWnd, WM_USER, 0, GoWasteSomeCPU() );
  37.                             break;
  38.                         case IDM_NORMAL:
  39.                             SetThreadPriority( hThread, THREAD_PRIORITY_NORMAL );
  40.                             SendMessage( hWnd, WM_USER, 0, GoWasteSomeCPU() );
  41.                             break;
  42.                         case IDM_HIGH:
  43.                             SetThreadPriority( hThread, THREAD_PRIORITY_ABOVE_NORMAL );
  44.                             SendMessage( hWnd, WM_USER, 0, GoWasteSomeCPU() );
  45.                             break;
  46.                         case IDM_BEST:
  47.                             SetThreadPriority( hThread, THREAD_PRIORITY_HIGHEST );
  48.                             SendMessage( hWnd, WM_USER, 0, GoWasteSomeCPU() );
  49.                             break;
  50.                         case IDM_EXIT:
  51.                             DestroyWindow(hWnd);
  52.                         break;
  53.                     }
  54.                 }
  55.                 break;
  56.             case WM_USER:
  57.                 {
  58.                     TCHAR szBuffer[128];
  59.                     static int nRow = 0;
  60.                     HDC hDC = GetDC( hWnd );
  61.                     wsprintf( szBuffer, "Priority for Thread %lX is %d. Test Speed = %d ",
  62.                               GetCurrentThreadId( ), GetThreadPriority( GetCurrentThread( ) ),
  63.                               lParam );
  64.                     TextOut( hDC, 0, nRow, szBuffer, lstrlen( szBuffer ) );
  65.                     ReleaseDC( hWnd, hDC );
  66.                     nRow += 20;
  67.                     if (nRow > 220)
  68.                        nRow = 0;
  69.                 }
  70.                 break;
  71.            case WM_DESTROY:
  72.                     PostQuitMessage( 0 );
  73.                     break;
  74.            default:
  75.                 return (DefWindowProc(hWnd, uMsg, wParam, lParam));
  76.     }
  77.     return (NULL);
  78. }